﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Sequence
{
    public partial class FrmOpenProcess : Form
    {
        public string sId ;
        public OleDbConnection m_connection;
        public FrmOpenProcess(OleDbConnection oConnection)
        {
            InitializeComponent();
            m_connection = oConnection;
        }

        public OleDbConnection GetConnection()
        {
            return m_connection;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmOpenProcess_Load(object sender, EventArgs e)
        {
            LoadProcess();
        }

        private void LoadProcess()
        {
            String sSql = "select * from t_api_qd_process ";

            try
            {
                OleDbCommand command = new OleDbCommand(sSql, GetConnection());
                OleDbDataReader oDataReader = command.ExecuteReader();
                listView1.Items.Clear();txtSelected.Text = ""; sId = "";
                while (oDataReader.Read())
                {
                    ListViewItem oListViewItem = listView1.Items.Add(oDataReader["ID"].ToString());
                    oListViewItem.SubItems[0].Text = oDataReader["ID"].ToString();
                    oListViewItem.SubItems.Add(oDataReader["NAME"].ToString());

                }
                oDataReader.Close();
                oDataReader.Dispose();
                command.Dispose();

            }
            catch (Exception ex)
            {
                //WriteError(ex.Message + " (" + sSql + ")");
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (listView1.SelectedItems.Count > 0)
                {

                    sId = listView1.SelectedItems[0].Text;
                    string sName = listView1.SelectedItems[0].SubItems[1].Text;
                    txtSelected.Text = sId + "\t" + sName;
                    
                }
            }
            catch (Exception ex)
            {
                //WriteError(ex.Message + " (" + sSql + ")");
            }
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            this.Close();
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                string sError = CSqlString.RunSql("delete from t_api_qd_process where id=" +listView1.SelectedItems[0].Text, GetConnection());
                sError = CSqlString.RunSql("delete from t_api_qd_process_step where process_id=" + listView1.SelectedItems[0].Text, GetConnection());
                sError = CSqlString.RunSql("delete from t_api_qd_process_step_params where process_id=" + listView1.SelectedItems[0].Text, GetConnection());
                LoadProcess();
            }
        }
    }
}
